// 初始化表单数据
export function initListenerForm(listener) {
    let self = {
        ...listener
    };
    if (listener.event === "timeout" && listener.eventDefinitions) {
        if (listener.eventDefinitions.length) {
            let k = "";
            for (let key in listener.eventDefinitions[0]) {
                console.log(listener.eventDefinitions, key);
                if (key.indexOf("time") !== -1) {
                    k = key;
                    self.eventDefinitionType = key.replace("time", "").toLowerCase();
                }
            }
            console.log(k);
            self.eventTimeDefinitions = listener.eventDefinitions[0][k].body;
        }
    }
    return self;
}

export function initListenerType(listener) {
    let listenerType;
    if (listener.class) listenerType = "classListener";
    if (listener.expression) listenerType = "expressionListener";
    if (listener.delegateExpression) listenerType = "delegateExpressionListener";
    return {
        ...JSON.parse(JSON.stringify(listener)),
        ...(listener.script ?? {}),
        listenerType: listenerType
    };
}

export function initCustomListener(listener) {
    if (listener.type) {
        listener.listenerType = listener.type;
    }
    if (listener.params) {
        listener.fields = listener.params;
        delete listener.params;
        listener.fields.forEach(field => {
            field.fieldType = field.type;
            if(field.type === 'string'){
                field.string = field.value;
            } else if(field.type === 'expression'){
                field.expression = field.value;
            }
        })
    }
    if (listener.type === 'classListener' || listener.listenerType === 'classListener') {
        listener.class = listener.value;
    } else if (listener.type === 'expressionListener' || listener.listenerType === 'expressionListener') {
        listener.expression = listener.value;
    } else if (listener.type === 'delegateExpressionListener' || listener.listenerType === 'delegateExpressionListener') {
        listener.delegateExpression = listener.value;
    }
    return listener;
}


export const listenerType = {
    classListener: "Java 类",
    expressionListener: "表达式",
    delegateExpressionListener: "代理表达式"
};

export const eventType = {
    start: "开始",
    end: "结束",
    take: "启用"
};

export const taskEventType = {
    create: "创建",
    assignment: "指派",
    complete: "完成",
    delete: "删除",
    update: "更新",
    timeout: "超时"
};

export const fieldType = {
    string: "字符串",
    expression: "表达式"
};
